/*
 * This file is part of the Try! Engine project.
 *
 * Copyright 2010-2011 Emanuele Bertoldi. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * You should have received a copy of the modified BSD License along with this
 * program. If not, see <http://www.opensource.org/licenses/bsd-license.php>
 */

#ifndef TRY_AUDIOMANAGER_H
#define TRY_AUDIOMANAGER_H

#include "audio.h"
#include <singleton.h>
#include <map>

#define audio AudioManager::instance()

namespace Try
{
    class BufferData;
    class SourceData;

    class AudioManager : public Singleton<AudioManager>
    {
    private:
        std::vector<BufferData*> m_buffers;
        std::vector<SourceData*> m_sources;

    public:
        AudioManager(float doppler_factor = 1.0f, float doppler_velocity = 343.0f);
        virtual ~AudioManager();

        // Doppler.
        float dopplerFactor() const;
        float dopplerVelocity() const;

        bool setDopplerFactor(float factor);
        bool setDopplerVelocity(float velocity);

        // Listener.
        float listenerGain() const;

        Vector3 listenerPosition() const;
        Vector3 listenerDirection() const;
        Vector3 listenerVelocity() const;

        bool setListenerGain(float gain);

        bool setListenerPosition(const Vector3& pos);
        bool setListenerDirection(const Vector3& dir);
        bool setListenerVelocity(const Vector3& vel);

        // Sources.
        int sourceCount() const;

        bool isLoaded(const Try::String& filename) const;
        bool isPlaying(SourceId source_id) const;

        float pitch(SourceId source_id) const;
        float gain(SourceId source_id) const;

        Vector3 position(SourceId source_id) const;
        Vector3 direction(SourceId source_id) const;
        Vector3 velocity(SourceId source_id) const;

        SourceId load(const Try::String& filename);
        bool release(SourceId source_id);

        bool play(SourceId source_id, bool loop = false, bool force_restart = true);
        bool pause(SourceId source_id);
        bool stop(SourceId source_id);
        bool stopAll();

        bool setPitch(SourceId source_id, float pitch);
        bool setGain(SourceId source_id, float gain);

        bool setPosition(SourceId source_id, const Vector3& pos);
        bool setDirection(SourceId source_id, const Vector3& dir);
        bool setVelocity(SourceId source_id, const Vector3& vel);

    protected:
        int findBuffer(const Try::String& filename);
    };
}

#endif // TRY_AUDIOMANAGER_H
